package com.hc.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.music.common.R;
import com.hc.music.dao.SingerMapper;
import com.hc.music.entity.Singer;
import com.hc.music.service.SingerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {
    @Autowired
    SingerMapper singerMapper;
    @Override
    public R addSinger(Singer addSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(addSingerRequest,singer);
        singer.setImage("img/singerPic/user.jpg");
        if(singerMapper.insert(singer) > 0){
            return R.success("新增歌手成功");
        }else {
            return R.error("新增歌手失败");
        }
    }

    @Override
    public R updateSingerMsg(Singer updateSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(updateSingerRequest,singer);
        if(singerMapper.updateById(singer) > 0){
            return R.success("修改歌手信息成功");
        }else {
            return R.error("修改歌手信息失败");
        }
    }

    @Override
    public R updateSingerPic(MultipartFile avatorFile, int id) {
        try {
            String filename = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String path = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"singerPic" +System.getProperty("file.separator")+ filename;
            File file = new File(path);
            avatorFile.transferTo(file);
            Singer singer = new Singer();
            singer.setId(id);
            String rp = "img/singerPic/"+ filename;
            singer.setImage(rp);
            if(singerMapper.updateById(singer) > 0){
                return R.success("上传成功",rp);
            }else {
                return R.error("上传失败");
            }
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    @Override
    public R deleteSinger(Integer id) {
        if(singerMapper.deleteById(id) > 0){
           return R.success("删除歌手成功");
        }else {
            return R.error("删除歌手失败");
        }
    }

    @Override
    public R allSinger() {
        return R.success(null,singerMapper.selectList(null));
    }

    @Override
    public R singerOfName(String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return R.success(null,singerMapper.selectList(queryWrapper));
    }

    @Override
    public R singerOfSex(Integer sex) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sex", sex);
        return R.success(null,singerMapper.selectList(queryWrapper));
    }
}
